package com.chukun.flink.stream.action.fraud.rules.utils;

import com.fasterxml.jackson.databind.ObjectMapper;

import java.io.IOException;

/**
 * @author chukun
 * @version 1.0.0
 * @description json序列化工具
 * @createTime 2022年05月22日 15:33:00
 */
public class JsonMapper<T> {

    private final  Class<T> targetClass;

    private final ObjectMapper objectMapper;

    public JsonMapper(Class<T> targetClass) {
        this.targetClass = targetClass;
        objectMapper = new ObjectMapper();
    }

    public T fromString(String value) throws IOException {
        return objectMapper.readValue(value, targetClass);
    }

    public String toString(T model) throws IOException {
        return objectMapper.writeValueAsString(model);
    }
}
